package com.example.bloodbankapplication.fragment;

import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.bloodbankapplication.R;
import com.example.bloodbankapplication.database.DonorStorage;
import com.example.bloodbankapplication.entity.PersonInformationModel;

public class RecentviewedDonorsFragment  extends BaseFragment{
	private ListView lv;
	private ArrayList<PersonInformationModel> recentViewedDonorList;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.donorslist, null);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		lv = (ListView) getView().findViewById(R.id.listview);
		
		DonorStorage donor = new DonorStorage(getActivity());
		
		recentViewedDonorList = donor.getViewedDonorList();
		
		lv.setAdapter(new DataDisplayAdapter(recentViewedDonorList));
				
	}
	
	
class DataDisplayAdapter extends BaseAdapter{
		
		//scope of global variable should be entire class
		private ArrayList<PersonInformationModel> donorsList;

		public DataDisplayAdapter(ArrayList<PersonInformationModel> list) {
			// scope of local variable should be with in the block;
			donorsList = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return donorsList.size();
		}

		@Override
		public Object getItem(int arg3) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		class ViewHolder {
			
			public TextView name,phno,lastname,bg,email;
		}

		@Override
		public View getView(int position, View v, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder holder;
			if(v == null){
				holder = new ViewHolder();
				LayoutInflater inflater = getActivity().getLayoutInflater();
				v = inflater.inflate(R.layout.datadisplay, null);
				holder.name = (TextView) v.findViewById(R.id.name);
				holder.phno = (TextView) v.findViewById(R.id.number);
				holder.lastname = (TextView) v.findViewById(R.id.lastname);
				holder.bg = (TextView) v.findViewById(R.id.email);
				holder.email = (TextView) v.findViewById(R.id.bloodgroup);
				
				holder.lastname.setVisibility(View.VISIBLE);
				holder.bg.setVisibility(View.VISIBLE);
				holder.email.setVisibility(View.VISIBLE);
				
				v.setTag(holder);
				
			}else{
				 holder = (ViewHolder) v.getTag();
			}
			
			
			PersonInformationModel model = donorsList.get(position);
			holder.name.setText(model.getName());
			holder.name.setTextColor(Color.RED);
			holder.phno.setText(model.getPhno());
			holder.phno.setTextColor(Color.GREEN);
			holder.lastname.setText(model.getLastname());
			holder.lastname.setTextColor(Color.GREEN);
			holder.bg.setText(model.getBloodGroup());
			holder.bg.setTextColor(Color.GREEN);
			holder.email.setText(model.getEmail());
			holder.email.setTextColor(Color.GREEN);
			
			return v;
		}
		
	}
	

}
